﻿/////////////////////////////////////////////////
//文件名:DataService
//描  述:
//创建者:刘凡平(Iveely Liu)
//邮  箱:945222889@qq.com
//组  织:Iveely
//年  份:2012/5/22 12:39:32
///////////////////////////////////////////////


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IveelySE.BigData.Common;

namespace IveelySE.BigData.StoreNode
{
    /// <summary>
    /// 数据操作服务
    /// </summary>
    public class DataService
    {
        
        public static PObject dealWithRead(byte[] msg)
        {
            PObject obj = (PObject)Common.Serialize.Byte2Obj(msg);
            //如果对象为空
            if (obj == null)
            {
                //则中断本次对话
                return new PObject("NULL", "为空");
            }
            //获取该文件的块
            Block block = (Block)LocalFile.GetObj(obj.fileName);
            //如果该块为空
            if (block == null)
            {
                //说明文件不存在，结束本次对话
                return new PObject("NULL", "为空");
            }
            //获取行号
            return block.Find(int.Parse(obj.value.ToString()));
        }

        public static PObject dealWithWrite(byte[] msg)
        {
            PObject Obj = (PObject)Common.Serialize.Byte2Obj(msg);

            //如果文件名为空
            if (Obj.fileName == "")
            {
                //直接返回
                return new PObject("NULL", "为空");
            }
            //获取该文件的块
            Block block = (Block)LocalFile.GetObj(Obj.fileName);
            //如果该块为空
            if (block == null)
            {
                //新建一个块
                block = new Block();
                //设定块的文件名
                block.FileName = Obj.fileName;
            }
            //块中增加对象数据
            block.Add(Obj, msg.Length);
            //将块写回对象文件
            LocalFile.SetObj(Obj.fileName, block);
            return new PObject("OK", "OK");
        }

        /// <summary>
        /// 读取数据服务
        /// </summary>
        public static void Read()
        {
            //字节容器数组
            byte[] msg = new byte[10240];
            //建立服务端
            Common.Communication.Transfer.Server server = new Common.Communication.Transfer.Server(Program.Config.MyIP, Program.Config.ReadPort);
            //不断监听
            while (true)
            {
                //启动监听
                server.Listen(ref msg, dealWithRead);
                //将用户传入的读取信息重组为对象

            }
        }

        /// <summary>
        /// 数据插入服务
        /// </summary>
        public static void Insert()
        {
            byte[] msg = new byte[10240];
            BigData.Common.Communication.Transfer.Server server = new Common.Communication.Transfer.Server(Program.Config.MyIP, Program.Config.WritePort);
            while (true)
            {
                //插入数据的信息格式
                //fileName:data
                server.Listen(ref msg, dealWithWrite);

            }
        }
        /// <summary>
        /// 登录到服务器
        /// </summary>
        public static void Login()
        {
            //建立访问客户端
            Common.Communication.Transfer.Client client = new Common.Communication.Transfer.Client(Program.Config.ServerIP, Program.Config.ConnectPort);
            //访问传输对象
            PObject obj = new PObject("CONNECT", Program.Config.MyIP.ToString() + "," + Program.Config.WritePort);

            //不断的在执行连接
            while (client.ConnectToServer(obj) == null) { };
        }


    }
}
